const fs = require('fs');
const http = require('http');
const path = require('path');
const express = require('express');
const morgan = require('morgan');
const rfs = require('rotating-file-stream');

const app = express();
const logDirectory = path.join(__dirname, '../log');

// console.log(logDirectory);

// fs.exists(logDirectory) || fs.mkdir(logDirectory);
// 判定文件路径存在
fs.stat(logDirectory, (err, stat) => {
    if (err) {
        console.log('不存在');
        fs.mkdir(logDirectory, (err) => {
            if (err) {
                console.log('未能创建' + err);
            } else {
                console.log('创建成功');
            }
        });
    } else {
        console.log('存在');
    }
});

let accessLogStream = rfs('access.log', {
    interval: '5m',
    path: logDirectory
});

app // 日志
    .use(morgan('combined', { stream: accessLogStream }))
    // 路由
    .get('/', function(req, res) {
        res.send('heelo word');
    })
    .get('/index', function(req, res) {
        res.send('will come to index');
    })
    // 监听端口
    .listen(3000);